package org.youshang.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.example.annotation.ConsumerKafkaMessage;
import org.example.annotation.ProducerKafkaMessage;
import org.example.config.LocalKafkaTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.youshang.entity.Student;
import org.youshang.service.HelloService;

/**
 *
 * @author youshang
 * @date 2021/02/09 15:34
 **/
@Service
public class HelloServiceImpl implements HelloService {


    @ProducerKafkaMessage("test")
    @Override
    public Student hello(String record) {
        if ("".equals(record) || null == record) {
            return new Student();
        }
        Student student = new Student();
        student.setName("zs");
        String sex = "";
        if (record.length() % 2 == 0) {
            sex = "男";
        }else {
            sex = "女";
        }
        student.setSex(sex);
        //发送消息
//        new LocalKafkaTemplate().send("test",record,student);
        return student;
    }

    /**
     * 消费消息
     * @param cord
     * @return
     */
    @ConsumerKafkaMessage(value = "test",consumerGroup = "dev-test1")
    public String consumerHello(ConsumerRecord<String, String> cord){
        if (null == cord || "".equals(cord)){
            return null;
        }
        //接受消息
        String result =  "topic =test,consumerGroup = dev-test1。    offset:"+ cord.offset() +" key:  "+ cord.key() + "   value: "+ cord.value();
        System.out.println(result);
        return result;
    }
    /**
     * 消费消息
     * @param cord
     * @return
     */
//    @ConsumerKafkaMessage(value = "test",consumerGroup = "dev-test2")
//    public String consumerHello2(ConsumerRecord<String, String> cord){
//        if (null == cord || "".equals(cord)){
//            return null;
//        }
//        //接受消息
//        String result =  "topic =test,consumerGroup = dev-test2。    offset:"+ cord.offset() +" key:  "+ cord.key() + "   value: "+ cord.value();
//        System.out.println(result);
//        return result;
//    }

    public void test(){
        System.out.println("test");
    }
}
